import {visceralFatJudge,basalMetabolismJudge,proteinJudge,bmiJudge,pbfJudge,muscleJudge,boneJudge,ageJudge,waterJudge,tznum,resetVal} from "../../utils/levelTool";
import {getBodyIndex,GetFatDetailList,GetLastFatDetail} from '../../api/api';
import getNextDate from '../../utils/getNextDate';
import {getCurrentDate} from '../../utils/timeTool';
Page({
  /**
   * 页面的初始数据
   */
  data: {
      activeIndex:0,
      isShow:[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],//展开了收起
      weightTime:'',
      getBodyIndexDataStorage:null,
      getBodyIndexData:[],
      analyzeData:{
        visceralFat: 0,//内脏脂肪指数
        basalMetabolism:0,//基础代谢量
        protein: 0,//蛋白质，单位:%
        bodyStyle: "",//体型
        age: 0,	//年龄
        weight: 0, //体重，单位:kg
        bmi: 0,//体质指数，bmi
        pbf: 0,//脂肪率， %
        rateOfMuscle: 0,//	肌肉量，单位:kg
        bone: 0,	//骨骼量, 单位:kg
        water: 0,	//水分率	
        bodyAge: 0,//身体年龄
      },
      weightLeft:0,
      weightRight:0,
      //达标数
      standardNum:0,
      //不达标数
      noStandardNum:0,
      //指标无数据
      noData:0,
      // 是否是达标
      compliance:{
        visceralFat:{
          standard:false,
          levelTitle:""
        },//内脏脂肪指数
        basalMetabolism:{
          standard:false,
          levelTitle:""
        },//基础代谢量
        protein: {
          standard:false,
          levelTitle:""
        },//蛋白质，单位:%
        bmi:{
          standard:false,
          levelTitle:""
        },//体质指数，bmi
        pbf:{
          standard:false,
          levelTitle:""
        },//脂肪率， %
        rateOfMuscle: {
          standard:false,
          levelTitle:""
        },//	肌肉量，单位:kg
        bone: {
          standard:false,
          levelTitle:""
        },	//骨骼量, 单位:kg
        water: {
          standard:false,
          levelTitle:""
        },	//水分率	
        bodyAge: {
          standard:false,
          levelTitle:""
        },//身体年龄
      },
      // 下降的体重
      declineWeight:"0",
      //日期
      bodyDate:"",
      bodyTime:"",




  },
  toggleActiveIndex(e){
    // currentTarget和target区别
    // 1.如果你点击的是添加事件元素的子元素，就用currentTarget来获取父元素的值，用target来获取子元素的值；
    // 2、如果没有子元素，就在本元素上添加事件，target和currentTarget效果一样。
    let activeIndex=e.target.dataset.val;
    this.setData({
      activeIndex,
    })
  },
  toggleShow(e){
    let id=e.currentTarget.dataset.index;
    this.data.isShow[id]=!this.data.isShow[id];
    this.setData({
      isShow:this.data.isShow
    })

  },
  judgeDataLevel(){
    let {analyzeData,getBodyIndexDataStorage}=this.data;
    resetVal();
    let bmi=bmiJudge(analyzeData.bmi); 
    if(getBodyIndexDataStorage!=null){
        getBodyIndex(getBodyIndexDataStorage,(e)=>{
            this.setData({
              getBodyIndexData:e.data.data.labelList
            })

            let {getBodyIndexData}=this.data;
            let visceralFat=visceralFatJudge(analyzeData.visceralFat,getBodyIndexData[5].levelIntervalList);
            let basalMetabolism=basalMetabolismJudge(analyzeData.basalMetabolism,getBodyIndexData[6].levelIntervalList)
            let protein=proteinJudge(analyzeData.protein,getBodyIndexData[11].levelIntervalList); //这个标准错误 裸脚秤下 试试范围值
            let pbf=pbfJudge(analyzeData.pbf,getBodyIndexData[1].levelIntervalList); 
            let rateOfMuscle=muscleJudge(analyzeData.rateOfMuscle,getBodyIndexData[8].levelIntervalList); 
            let bone=boneJudge(analyzeData.bone,getBodyIndexData[10].levelIntervalList); 
            let bodyAge=ageJudge(analyzeData.age,analyzeData.bodyAge,analyzeData.basalMetabolism); 
            let water=waterJudge(analyzeData.water,getBodyIndexData[9].levelIntervalList); 
            let tznumVal=tznum(); 
        
            let compliance={
              visceralFat,//内脏脂肪指数
              basalMetabolism,//基础代谢量
              protein,//蛋白质，单位:%
              bmi,//体质指数，bmi
              pbf,//脂肪率， %
              rateOfMuscle,//	肌肉量，单位:kg
              bone,	//骨骼量, 单位:kg
              water,	//水分率	
              bodyAge,//身体年龄
            };
            this.setData({
              standardNum:tznumVal.standardNum,
              noStandardNum:tznumVal.noStandardNum,
              noData:tznumVal.noData,
              compliance,
              analyzeData
            })

        })
    }else{

      let visceralFat=visceralFatJudge(analyzeData.visceralFat);
      let basalMetabolism=basalMetabolismJudge(analyzeData.basalMetabolism)
      let protein=proteinJudge(analyzeData.protein); //这个标准错误 裸脚秤下 试试范围值

      let pbf=pbfJudge(analyzeData.pbf); 
      let rateOfMuscle=muscleJudge(analyzeData.rateOfMuscle); 
      let bone=boneJudge(analyzeData.bone); 
      let bodyAge=ageJudge(analyzeData.age,analyzeData.bodyAge,analyzeData.basalMetabolism); 
      let water=waterJudge(analyzeData.water); 
      let tznumVal=tznum(); 
  
      let compliance={
        visceralFat,//内脏脂肪指数
        basalMetabolism,//基础代谢量
        protein,//蛋白质，单位:%
        bmi,//体质指数，bmi
        pbf,//脂肪率， %
        rateOfMuscle,//	肌肉量，单位:kg
        bone,	//骨骼量, 单位:kg
        water,	//水分率	
        bodyAge,//身体年龄
      };
      this.setData({
        standardNum:tznumVal.standardNum,
        noStandardNum:tznumVal.noStandardNum,
        noData:tznumVal.noData,
        compliance,
        analyzeData

      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.val);
    let currentData=JSON.parse(options.val);
      if (currentData) {
        // console.log(currentData);
        // 和上次的体重比较
        let declineWeight=0;
        if(currentData.lastWeight){
           declineWeight=(currentData.weight-currentData.lastWeight).toFixed(1);
        }
        let myweight=String(currentData.weight).split('.');
        
        if(currentData.resistance50k!=0){
          let getBodyIndexDataStorageHQ={
                age:currentData.age,
                sex:currentData.sex,
                weight:currentData.weight,
                height:currentData.height,
                resistance:currentData.resistance50k
          }
          // let analyzeData24={
          //   visceralFat: 6,//内脏脂肪指数
          //   basalMetabolism:12313,//基础代谢量
          //   protein: 10,//蛋白质，单位:%
          //   bodyStyle: "标准型",//体型
          //   age: 26,	//年龄
          //   weight: 15, //体重，单位:kg
          //   bmi: 234,//体质指数，bmi
          //   pbf: 234,//脂肪率， %
          //   rateOfMuscle: 34,//	肌肉量，单位:kg
          //   bone: 44,	//骨骼量, 单位:kg
          //   water: 44,	//水分率	
          //   bodyAge: 44,//身体年龄
          // }

          this.setData({
              weightLeft:myweight[0],
              weightRight:myweight[1],
              analyzeData:currentData,
              declineWeight,
              bodyDate:currentData.createdate.substring(0,10),
              bodyTime:currentData.createdate.substring(11,16),
              getBodyIndexDataStorage:getBodyIndexDataStorageHQ
          })
        }else{

          this.setData({
              weightLeft:myweight[0],
              weightRight:myweight[1],
              analyzeData:currentData,
              declineWeight,
              bodyDate:currentData.createdate.substring(0,10),
              bodyTime:currentData.createdate.substring(11,16),
          })
        }
       }
    this.judgeDataLevel();



      //获取最近的一次秤重数据
      // let TZCuserInfo=wx.getStorageSync("TZCuserInfo");
      //  if (TZCuserInfo) {
      //   wx.showLoading({
      //     title: '加载中',
      //     mask:true
      //   })

      //   GetLastFatDetail(TZCuserInfo.id,(e)=>{
      //     wx.hideLoading();
      //     if(e.data){
      //       console.log(e.data);
      //       let currentData= e.data;//获取最近一次的称重数据   //reverse会影响原来的数组
      //       if (currentData) {
      //         let declineWeight=0;
      //         if(e.data[1]){//reverse会影响原来的数组 所以这里不用颠倒数组顺序
      //           let currentData2= e.data[1];//倒数第二次数据
      //            declineWeight=(currentData.weight-currentData2.weight).toFixed(1);
      //         }
      //         let myweight=String(currentData.weight).split('.');
              
      //         if(currentData.resistance50k!=0){
      //           let getBodyIndexDataStorageHQ={
      //                 age:currentData.age,
      //                 sex:currentData.sex,
      //                 weight:currentData.weight,
      //                 height:currentData.height,
      //                 resistance:currentData.resistance50k
      //           }
      //           // let analyzeData24={
      //           //   visceralFat: 6,//内脏脂肪指数
      //           //   basalMetabolism:12313,//基础代谢量
      //           //   protein: 10,//蛋白质，单位:%
      //           //   bodyStyle: "标准型",//体型
      //           //   age: 26,	//年龄
      //           //   weight: 15, //体重，单位:kg
      //           //   bmi: 234,//体质指数，bmi
      //           //   pbf: 234,//脂肪率， %
      //           //   rateOfMuscle: 34,//	肌肉量，单位:kg
      //           //   bone: 44,	//骨骼量, 单位:kg
      //           //   water: 44,	//水分率	
      //           //   bodyAge: 44,//身体年龄
      //           // }
  
      //           this.setData({
      //               weightLeft:myweight[0],
      //               weightRight:myweight[1],
      //               analyzeData:currentData,
      //               declineWeight,
      //               bodyDate:currentData.createdate.substring(0,10),
      //               bodyTime:currentData.createdate.substring(11,16),
      //               getBodyIndexDataStorage:getBodyIndexDataStorageHQ
      //           })
      //         }else{

      //           this.setData({
      //               weightLeft:myweight[0],
      //               weightRight:myweight[1],
      //               analyzeData:currentData,
      //               declineWeight,
      //               bodyDate:currentData.createdate.substring(0,10),
      //               bodyTime:currentData.createdate.substring(11,16),
      //           })
      //         }
      //       }
      //     }
      //     this.judgeDataLevel();
      //   })


      //   }
        


          
  
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})